/*
  axios.get() 查
  axios.post() 增
  axios.put() 改
  axios.delete() 删
*/
//Axios构造函数==类名，首字母大写
class Axios{
  get(url,data,formType) {
    return this.request('get',url,data,formType);
  }
  post(url,data,formType){
    return this.request('post',url,data,formType);
  }
  put(url,data,formType){
    return this.request('put',url,data,formType);
  }
  delete(url,data,formType) {
    return this.request('delete',url,data,formType);
  }
  request(method,url,data,formType){
    //promise
    return new Promise((resolve, reject) => {
      let contentType = formType ? "application/x-www-form-urlencoded" : "application/json";
      wx.request({
        url: url,
        method : method,
        data: data,
        header:{
          "content-type" : contentType
        },
        success(res) {
          //成功的回调
          resolve(res);
        },
        fail(err) {
          //失败的回调
          reject(err);
        }
    });
    
    })
  }
}

//导出模块：实例
module.exports = new Axios();